import { computed, ref } from 'vue';
import { defineStore } from 'pinia';

const useTagsStore = defineStore(
  'tags',
  () => {
    let list = ref([]);

    let show = computed(() => {
      return list.value.length > 0;
    });
    let nameList = computed(() => {
      return list.value.map((item) => item.name);
    });

    function delTagsItem(index) {
      list.value.splice(index, 1);
    }

    function setTagsItem(data) {
      list.value.push(data);
    }

    function clearTags() {
      list.value = [];
    }

    function closeTagsOther(data) {
      list.value = data;
    }

    return { list, show, nameList, delTagsItem, setTagsItem, clearTags, closeTagsOther };
  },
  {
    persist: {
      storage: sessionStorage,
    },
  },
);

export default useTagsStore;
